﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using System.Xml;
using AMS.Web.WorkBill;
using VitCode.BaseDataPageCreator;
using VitCode.BaseDataPageCreator.Setting;
using VitCode.Castle.DataAccess;
using VitCode.Web.UI;
using VitCode.Web.UI.WebControls;

namespace AMS.Web.AssetQuery
{
    public partial class WorkBillList : System.Web.UI.Page
    {
        private readonly CastleAccess m_Acc = new CastleAccess();
        private string sql = "";
        private ObjXmlReader m_XmlReader;
        private readonly CreateControlHelper m_CreateCtlHlp = new CreateControlHelper();
        private PageBuilder m_PageBuilder;
        private WoPageBuilder m_WoPageBuilder;

        private string QueryCondition
        {
            get
            {

                if (ViewState["QueryCondition"] != null)
                    return ViewState["QueryCondition"].ToString();
                else
                    return "";
            }
            set { ViewState["QueryCondition"] = value; }
        }

        private string SettingXmlPath
        {
            get { return AppDomain.CurrentDomain.BaseDirectory + "AssetQuery\\ProSetting.xml"; }
        }

        private string WorkBillSettingXmlPath
        {
            get { return AppDomain.CurrentDomain.BaseDirectory + "Setting\\WoSetting.xml"; }
        }

        public string MenuId
        {
            get { return Request.QueryString["MenuId"]; }
        }

        private string ClassName
        {
            get { return m_CreateCtlHlp.ReturnQueryStringValue("ClassName", this); }
        }

        private string TypeName
        {
            get { return m_CreateCtlHlp.ReturnQueryStringValue("TypeName", this); }
        }

        protected override void OnInit(EventArgs e)
        {
            if (m_PageBuilder == null)
                m_PageBuilder = new PageBuilder(this, SettingXmlPath);
            if (m_WoPageBuilder == null)
                m_WoPageBuilder = new WoPageBuilder(Page, WorkBillSettingXmlPath);
            if (TypeName.Length > 0)
                m_PageBuilder.SetPgListAttribute(ListGridInfo, ClassName, TypeName);
            else
                m_PageBuilder.SetPgListAttribute(ListGridInfo, ClassName);
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>");
            ListGridInfo.DataSourceDelegate = new PaginationGrid.GridDataSourceDelegate(BindPgList);
            if (Request.Cookies["Code"] == null)
            {
                Response.Redirect("../Default.aspx");
            }
            if (!IsPostBack)
            {
                m_CreateCtlHlp.SetDdlCondition(ListGridInfo, ddlCondition);
                m_XmlReader = new ObjXmlReader(SettingXmlPath);
                XmlNode node = m_XmlReader.GetXmlNode("Setting/" + ClassName);
                if (node != null)
                {
                    var strNavigate = m_XmlReader.GetNodeAttributeValue(node, "Memo", true);
                    NavigateSpan.InnerHtml = strNavigate;
                }
                if (ClassName == "Barcode")
                {
                    ConditionSelector1.Visible = false;
                    ddlCondition.Items.Clear();
                    ddlCondition.Items.Add(new ListItem("条码值", "Barcode"));
                    ddlCondition.Items.Add(new ListItem("条码段", ""));
                }

            }
        }



        private void ListBind()
        {
            ListGridInfo.BindGrid();

        }

        private object BindPgList()
        {
            m_XmlReader = new ObjXmlReader(SettingXmlPath);
            XmlNode node = m_XmlReader.GetXmlNode("Setting/" + ClassName + "/ListSql");
            if (node != null)
            {
                sql = m_XmlReader.GetNodeAttributeValue(node, "sql", true);
                sql += " " + QueryCondition;
                DataTable dt = m_Acc.GetDataTableBySql(sql);
                return dt;
            }
            return null;
        }





        protected void btnQuery_Click1(object sender, EventArgs e)
        {
            string userId = Request.Cookies["LoginUserId"].Value;
            string userGroupId = Request.Cookies["UserGroupId"].Value;
            if (ClassName == "Barcode")
            {
                if (ddlCondition.SelectedValue == "Barcode")
                {
                    QueryCondition = QueryHelper.GetQueryCondition(ddlCondition.SelectedValue, txtValue.Text, rbtAmount.Checked);

                    if (QueryCondition.Length > 0)
                     {
                         if (userGroupId.Trim().Length > 0)
                         {
                             if (!m_Acc.CheckIsAdmin(userId))
                                 QueryCondition += " and ai.UserGroupId = '" + userGroupId + "'";
                         }
                     }
                     else
                     {
                         if (userGroupId.Trim().Length > 0)
                         {
                             if (!m_Acc.CheckIsAdmin(userId))
                                 QueryCondition += " where ai.UserGroupId = '" + userGroupId + "'";
                         }
                     }
                }
                else
                {

                    string strStartValue = txtValue.Text.Trim();
                    string strEndValue = txtEndValue.Text.Trim();
                    if (strStartValue.Trim().Length > strEndValue.Trim().Length)
                    {
                        QueryCondition = " where 1 = 2";
                    }
                    else
                    {

                        QueryCondition = " where Barcode between '" + strStartValue +
                                      "' and '" + strEndValue + "'";

                        if (userGroupId.Trim().Length > 0)
                        {
                            if (!m_Acc.CheckIsAdmin(userId))
                                QueryCondition += " and ai.UserGroupId = '" + userGroupId + "'";
                        }
                    }


                }
            }
            else
            {
                QueryCondition = QueryHelper.GetQueryCondition(ddlCondition.SelectedValue, txtValue.Text, rbtAmount.Checked);
                if (ClassName == "NewAddAsset")
                {
                    if (QueryCondition.Length > 0)
                    {
                        if (userGroupId.Trim().Length > 0)
                        {
                            if (!m_Acc.CheckIsAdmin(userId))
                                QueryCondition += " and UserGroupId = '" + userGroupId + "'";
                        }
                    }
                    else
                    {
                        if (userGroupId.Trim().Length > 0)
                        {
                            if (!m_Acc.CheckIsAdmin(userId))
                                QueryCondition += " where UserGroupId = '" + userGroupId + "'";
                        }
                    }
                }
              
            }

            ListBind();
        }

        protected void ConditionSelector1_SelectCondition(SelectConditionEventArgs e)
        {
            string userId = Request.Cookies["LoginUserId"].Value;
            string userGroupId = Request.Cookies["UserGroupId"].Value;
            QueryCondition = null;
            if (ConditionSelector1.SelectedConditionString.Length > 0)
            {
                QueryCondition = " where " + ConditionSelector1.SelectedConditionString;
                if (userGroupId.Trim().Length > 0 && ClassName == "NewAddAsset")
                {
                    if (!m_Acc.CheckIsAdmin(userId))
                        QueryCondition += " and UserGroupId = '" + userGroupId + "'";
                }
            }
            else
            {
                if (userGroupId.Trim().Length > 0 && ClassName == "NewAddAsset") 
                {
                    if (!m_Acc.CheckIsAdmin(userId))
                        QueryCondition += " where UserGroupId = '" + userGroupId + "'";
                }
            }
              
            ListBind();
        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ClassName == "Barcode")
            {
                if (ddlCondition.SelectedItem.Value == "Barcode")
                {

                    labelStart.Visible = false;
                    labelEnd.Visible = false;
                    txtEndValue.Visible = false;
                    rbtLike.Visible = true;
                    rbtAmount.Visible = true;
                }
                else
                {
                    labelStart.Visible = true;
                    labelEnd.Visible = true;
                    txtEndValue.Visible = true;
                    rbtLike.Visible = false;
                    rbtAmount.Visible = false;
                }
            }

        }

        protected void ListGridInfo_OnGridItemSelect(object sender, PaginationGridItemSelectArgs e)
        {
            string key = e.LastClickColumnIndex;
            string val = e.SelectedDataKeyValue;
            if (ClassName == "WorkBill")
            {
                sql = "select a.TypeName from AmsWorkBill_WoInfo a left outer join AmsWorkBill_WoCommand b on a.Code = b.WoCode where a.Id='" + val + "'";
                var dt = m_Acc.GetDataTableBySql(sql);
                if (dt != null && dt.Rows.Count > 0)
                {
                    var className = dt.Rows[0][0].ToString();
                    var win = new Window(Page, "工单明细") { Height = 700, Width = 1000, Scrollbars = true, Status = true };
                    win.ShowModalDialog("WorkBillInfoDetail.aspx?ClassName=" + className + "Sql&WorkBillId=" + val, new[] { "hdCheckInfo" });
                }

            }
            else if (ClassName == "NewAddAsset" || ClassName == "Barcode")
            {
                 m_WoPageBuilder.ShowAssetInfoByWoCommandId(e.SelectedDataKeyValue, Page);
                 m_WoPageBuilder.ShowAssetInfoByAssetId(e.SelectedDataKeyValue, Page);
            }
            else
            {
                if (key == "1")
                {

                    var win = new Window(Page, "工单明细") { Height = 700, Width = 1000, Scrollbars = true, Status = true };
                    win.ShowModalDialog("WorkBillInfoDetail.aspx?ClassName=" + ClassName + "Sql&Id=" + val, new[] { "hdCheckInfo" });
                }
                else
                {
                    m_WoPageBuilder.ShowAssetInfoByWoCommandId(e.SelectedDataKeyValue, Page);
                }
            }


        }
    }
}
